<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@ page import="java.net.URLDecoder" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
    String etlUrl = (String) request.getAttribute("etlUrl");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <base href="<%=basePath%>">

    <title>数据源配置</title>
    <%@include file="head.jsp" %>
</head>
<body>

<div style="padding:5px;" id="tb">
    <a class="easyui-linkbutton" iconCls="icon-add" onclick="add()">新增数据源</a>&nbsp;&nbsp;
    <a class="easyui-linkbutton" iconCls="icon-edit" onclick="edit()">修改数据源</a>&nbsp;&nbsp;
</div>


<table id="dg" style="height: 400px;"></table>

<div style="visibility: hidden;">
    <div id="addDialog" class="easyui-dialog" data-options="iconCls:'icon-add',toolbar:'#tbForAdd'"
         style="width: 700px; height: 450px;top:100px;">
        <!--对话框的工具栏  -->
        <div id="tbForAdd" style="padding: 5px;height:28px;padding-top:5px">
            <a class="easyui-linkbutton" data-options="iconCls:'icon-ok'" onclick="testAdd()"
               id="testAddBtn">测试连接</a>
            <a class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick="InfoAdd()" id="addBtn">提交</a>
        </div>
        <form id="FormAdd" method="post">
            <table style="padding-left: 150px;padding-top:30px; padding-right: 30px">
                <tr>
                    <td align="right">数据源名称：</td>
                    <td>
                        <input data-options="multiline:true,validType:'length[1,60]',required:true"
                               class="easyui-validatebox" name="dataSourceName" id="dataSourceName_a"
                               style="height: 23px; width: 225px" missingMessage="该项不能为空"/>
                    </td>
                </tr>
                <tr>
                    <td align="right">ip地址：</td>
                    <td>
                        <input data-options="multiline:true,validType:'length[1,60]',required:true"
                               class="easyui-validatebox" name="ipAddress" id="ipAddress_a"
                               style="height: 23px; width: 225px" missingMessage="该项不能为空"/>
                    </td>
                </tr>
                <tr>
                    <td align="right">端口号：</td>
                    <td>
                        <input data-options="multiline:true,validType:'length[1,60]',required:true"
                               class="easyui-validatebox" name="portNumber" id="portNumber_a"
                               style="height: 23px; width: 225px" missingMessage="该项不能为空"/>
                    </td>
                </tr>
                <tr>
                    <td align="right">数据库名称：</td>
                    <td><input data-options="multiline:true,validType:'length[1,60]',required:true"
                               class="easyui-validatebox" name="databaseName" id="databaseName_a"
                               style="height: 23px; width: 225px" missingMessage="该项不能为空"/></td>
                </tr>
                <tr>
                    <td align="right">数据库类型：</td>
                    <td>
                        <%-- <input data-options="multiline:true,validType:'length[1,60]',required:true"
                                class="easyui-validatebox" name="databaseType" id="databaseType_a"
                                style="height: 23px; width: 225px"/>--%>
                        <select class='easyui-combobox' id='databaseType_a' name='databaseType' editable="false"
                                style='height: 32px;width:181px;'
                                data-options="required:true,url: 'queryDataDictionary?type=3',method: 'get',valueField:'code',
                            textField:'text'"></select>
                    </td>
                    <input type="hidden" id="id_a" name="id"/>
                </tr>


                <tr>
                    <td align="right">用户名称：</td>
                    <td><input data-options="multiline:true,validType:'length[1,60]',required:true"
                               class="easyui-validatebox" name="userName" id="userName_a"
                               style="height: 23px; width: 225px" missingMessage="该项不能为空"/></td>
                </tr>

                <tr>
                    <td align="right">密码：</td>
                    <td><input data-options="multiline:true,validType:'length[1,60]',required:true"
                               class="easyui-validatebox" name="password" id="password_a"
                               style="height: 23px; width: 225px" missingMessage="该项不能为空"/></td>
                </tr>
            </table>
        </form>
    </div>
</div>


<!--修改-->
<div style="visibility: hidden;">
    <div id="editDialog" class="easyui-dialog" data-options="iconCls:'icon-edit',toolbar:'#tbForEdit'"
         style="width: 700px; height: 450px;top:100px;">
        <!--对话框的工具栏  -->
        <div id="tbForEdit" style="padding: 5px;height:28px;padding-top:5px">
            <a class="easyui-linkbutton" data-options="iconCls:'icon-ok'" onclick="testEdit()"
               id="testEditBtn">测试连接</a>
            <a class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick="InfoEdit()" id="editBtn">提交</a>
        </div>
        <form id="FormEdit" method="post">
            <table style="padding-left: 150px;padding-top:30px; padding-right: 30px">
                <tr>
                    <td align="right">数据源名称：</td>
                    <td>
                        <input data-options="multiline:true,validType:'length[1,60]',required:true"
                               class="easyui-validatebox" name="dataSourceName" id="dataSourceName_e"
                               style="height: 23px; width: 225px" missingMessage="该项不能为空"/>
                    </td>
                </tr>
                <tr>
                    <td align="right">ip地址：</td>
                    <td>
                        <input data-options="multiline:true,validType:'length[1,60]',required:true"
                               class="easyui-validatebox" name="ipAddress" id="ipAddress_e"
                               style="height: 23px; width: 225px" missingMessage="该项不能为空"/>
                    </td>
                </tr>
                <tr>
                    <td align="right">端口号：</td>
                    <td>
                        <input data-options="multiline:true,validType:'length[1,60]',required:true"
                               class="easyui-validatebox" name="portNumber" id="portNumber_e"
                               style="height: 23px; width: 225px" missingMessage="该项不能为空"/>
                    </td>
                </tr>
                <tr>
                    <td align="right">数据库名称：</td>
                    <td><input data-options="multiline:true,validType:'length[1,60]',required:true"
                               class="easyui-validatebox" name="databaseName" id="databaseName_e"
                               style="height: 23px; width: 225px" missingMessage="该项不能为空"/></td>
                </tr>
                <tr>
                    <td align="right">数据库类型：</td>
                    <td>
                        <select class='easyui-combobox' id='databaseType_e' name='databaseType' editable="false"
                                style='height: 32px;width:181px;'
                                data-options="required:true,url: 'queryDataDictionary?type=3',method: 'get',valueField:'code',
                            textField:'text'"></select>
                    </td>
                    <input type="hidden" id="id_e" name="id"/>
                </tr>


                <tr>
                    <td align="right">用户名称：</td>
                    <td><input data-options="multiline:true,validType:'length[1,60]',required:true"
                               class="easyui-validatebox" name="userName" id="userName_e"
                               style="height: 23px; width: 225px" missingMessage="该项不能为空"/></td>
                </tr>

                <tr>
                    <td align="right">密码：</td>
                    <td><input data-options="multiline:true,validType:'length[1,60]',required:true"
                               class="easyui-validatebox" name="password" id="password_e"
                               style="height: 23px; width: 225px" missingMessage="该项不能为空"/></td>
                </tr>
            </table>
        </form>
    </div>
</div>


<script type="text/javascript">

    $(function () {
        $('#editDialog').window('close');
        $('#addDialog').window('close');

        $("#dg").datagrid({
            loadMsg: '数据加载中，请稍后...',
            url: '${serverUrl}/sys_dataSourceData',
            checkOnSelect: true,
            autoRowHeight: false,        //自动行高
            rownumbers: true,            //开启行号
            fitColumns: true,
            pagination: true,     //开启分页
            fit: true,
            toolbar: '#tb',
            pageSize: 25,
            pageList: [15, 25, 35],
            columns: [[
                {field: 'id', title: '编号', align: 'center', width: 80, checkbox: true},
                {field: 'dataSourceName', title: '数据源名称', align: 'center', width: 80},
                {field: 'ipAddress', title: 'ip地址', align: 'center', width: 80},
                {field: 'portNumber', title: '端口号', width: 80, align: 'center'},
                {field: 'databaseName', title: '数据库名称', width: 80, align: 'center'},
                {field: 'databaseType', title: '数据库类型', width: 80, align: 'center'},
                {field: 'userName', title: '用户名称', width: 80, align: 'center'},
                {field: 'password', title: '密码', width: 80, align: 'center'}
            ]]
        });
    });

    $('#addDialog').dialog({
        onClose: function () {
            $('.validatebox-tip').remove();
        }
    });
    $('#editDialog').dialog({
        onClose: function () {
            $('.validatebox-tip').remove();
        }
    });


    function add() {
        //加载表单
        $("#FormAdd").form('clear');

        //最后打开窗口
        $("#addDialog").window({title: "新增数据源"});
        $('#addDialog').window('open');
        $('#addDialog').window({modal: true});
        $('#addBtn').linkbutton('enable'); // 可用（取消置灰）
    }


    //保存
    function InfoAdd() {
        var dataSourceName = $('#dataSourceName_a').val().trim();
        var ipAddress = $('#ipAddress_a').val().trim();
        var portNumber = $('#portNumber_a').val().trim();
        var databaseName = $('#databaseName_a').val().trim();
        var databaseType = $('#databaseType_a').combobox("getValue");
        var userName = $('#userName_a').val().trim();
        var password = $('#password_a').val().trim();

        if ($('#addDialog').form('validate')) {
            var data = {
                "dataSourceName": dataSourceName,
                "ipAddress": ipAddress,
                "portNumber": portNumber,
                "databaseName": databaseName,
                "databaseType": databaseType,
                "userName": userName,
                "password": password
            };

            $.ajax({
                url: '${serverUrl}/addSys_dataSource',
                type: 'POST',
                dataType: 'JSON',
                contentType: "application/json",
                data: JSON.stringify(data),
                success: function (res) {
                    if (res.code == "000000") {
                        $.messager.alert('提示', "处理成功", "info");
                    } else {
                        $.messager.alert('提示', res.msg, "error");
                    }
                    $("#addDialog").dialog('close');
                    $('#dg').datagrid('reload');
                },
                error: function (error) {
                    $.messager.alert('提示', "处理失败", "error");
                }
            })
        }
    }

    function testAdd() {
        var ipAddress = $('#ipAddress_a').val().trim();
        var portNumber = $('#portNumber_a').val().trim();
        var databaseName = $('#databaseName_a').val().trim();
        var databaseType = $('#databaseType_a').combobox("getValue");
        var userName = $('#userName_a').val().trim();
        var password = $('#password_a').val().trim();

        if ($('#addDialog').form('validate')) {

            $.messager.progress({
                title: '请稍等',
                msg: '测试连接......'
            });

            var data = {
                "ipAddress": ipAddress,
                "portNumber": portNumber,
                "databaseName": databaseName,
                "databaseType": databaseType,
                "userName": userName,
                "password": password
            };

            $.ajax({
                url: '${serverUrl}/testSys_dataSource',
                type: 'POST',
                contentType: "application/json",
                data: JSON.stringify(data),
                success: function (res) {
                    $.messager.progress("close");
                    $.messager.alert('', res, 'info', function (r) {
                    });
                },
                error: function (error) {
                    $.messager.progress("close");
                    $.messager.alert('提示', "处理失败", "error");
                }
            })
        }
    }

    function edit() {
        var rowData = $('#dg').datagrid('getSelections');
        if (rowData.length != 1) { //如果选中
            $.messager.alert("操作提示", "请选择一条数据进行修改操作！", 'error');
        } else {
            //加载表单
            $("#FormEdit").form('clear');
            $("#FormEdit").form('load', rowData[0]);

            //最后打开窗口
            $("#editDialog").window({title: "修改数据源"});
            $('#editDialog').window('open');
            $('#editDialog').window({modal: true});
            $('#editBtn').linkbutton('enable'); // 可用（取消置灰）
        }

    }


    function InfoEdit() {
        var id = $('#id_e').val().trim();
        var dataSourceName = $('#dataSourceName_e').val().trim();
        var ipAddress = $('#ipAddress_e').val().trim();
        var portNumber = $('#portNumber_e').val().trim();
        var databaseName = $('#databaseName_e').val().trim();
        var databaseType = $('#databaseType_e').combobox("getValue");

        var userName = $('#userName_e').val().trim();
        var password = $('#password_e').val().trim();

        if ($('#FormEdit').form('validate')) {

            if ($('#editDialog').form('validate')) {
                var data = {
                    "id": id,
                    "dataSourceName": dataSourceName,
                    "ipAddress": ipAddress,
                    "portNumber": portNumber,
                    "databaseName": databaseName,
                    "databaseType": databaseType,
                    "userName": userName,
                    "password": password
                };

                $.ajax({
                    url: '${serverUrl}/editSys_dataSource',
                    type: 'POST',
                    dataType: 'JSON',
                    contentType: "application/json",
                    data: JSON.stringify(data),
                    success: function (res) {
                        if (res.code == "000000") {
                            $.messager.alert('提示', "处理成功", "info");
                        } else {
                            $.messager.alert('提示', res.msg, "error");
                        }
                        $("#editDialog").dialog('close');
                        $('#dg').datagrid('reload');
                    },
                    error: function (error) {
                        $.messager.alert('提示', "处理失败", "error");
                    }
                })
            }
        }
    }

    function testEdit() {
        var id = $('#id_e').val().trim();
        var ipAddress = $('#ipAddress_e').val().trim();
        var portNumber = $('#portNumber_e').val().trim();
        var databaseName = $('#databaseName_e').val().trim();
        var databaseType = $('#databaseType_e').combobox("getValue");
        var userName = $('#userName_e').val().trim();
        var password = $('#password_e').val().trim();

        if ($('#FormEdit').form('validate')) {

            $.messager.progress({
                title: '请稍等',
                msg: '测试连接......'
            });

            var data = {
                "id": id,
                "ipAddress": ipAddress,
                "portNumber": portNumber,
                "databaseName": databaseName,
                "databaseType": databaseType,
                "userName": userName,
                "password": password
            };

            $.ajax({
                url: '${serverUrl}/testSys_dataSource',
                type: 'POST',
                contentType: "application/json",
                data: JSON.stringify(data),
                success: function (res) {
                    $.messager.progress("close");
                    $.messager.alert('', res, 'info', function (r) {
                    });
                },
                error: function (error) {
                    $.messager.progress("close");
                    $.messager.alert('提示', "处理失败", "error");
                }
            })
        }
    }


    //删除
    function del() {
        var rowData = $('#dg').datagrid('getSelections');
        if (rowData.length > 0) { //如果选中
            var deldatas = $('#dg').datagrid('getSelections');
            var ids = '';
            for (var dataIndex in deldatas) {
                ids = ids + deldatas[dataIndex].id + ',';
            }
            ids = ids.substring(0, ids.length - 1);
            $.messager.confirm("确认删除", "确认删除数据吗？", function (r) {
                if (r)
                    $.post('PETLDelete', {
                        ids: ids,
                        "randSession": "${authId}",
                    }, function (r) {
                        $.messager.alert('提示', "删除成功", 'info', function (r) {
                            $('#dg').datagrid('reload');
                        });

                    });

            });
        } else {
            $.messager.alert("操作提示", "请选择一条数据进行删除操作！", 'error');
        }

    }

    //点击查询按钮
    function query() {
        var select = $("#select").combobox("getValue");
        $("#dg").datagrid("load", {select: select});
    }
</script>
</body>
</html>